package com.vidmt.api.modules.sys.dao;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.thinkgem.jeesite.common.persistence.CrudDao;
import com.thinkgem.jeesite.common.persistence.annotation.MyBatisDao;
import com.vidmt.api.modules.sys.Enums.OrderStatus;
import com.vidmt.api.modules.sys.entity.Order;

@MyBatisDao
public interface OrderDao extends CrudDao<Order> {
	@Select("SELECT * FROM v_order WHERE id=#{0}")
	public Order load(String id);

	@Select("SELECT * FROM v_order WHERE uid=#{0}")
	public List<Order> findByUid(Long uid);
	
	@Select("SELECT * FROM v_order WHERE trade_no=#{0}")
	public Order findByTradeno(String tradeno);

	@Override
	@Insert("INSERT INTO v_order(id,uid,skus,subject,attach,total_fee,real_fee,pay_type,status,create_time)"
			+ "VALUES(#{id},#{uid},#{skuStr},#{subject},#{attach},#{totalFee},#{realFee},#{payType},#{status},now())")
	public int insert(Order entity);
	
	@Override
	@Update("UPDATE v_order SET trade_no=#{tradeNo},pay_acc=#{payAcc},status=#{status},pay_time=#{payTime},refund_time=#{refundTime} WHERE id=#{id}")
	public int update(Order entity);

	@Update("UPDATE v_order SET status=#{0} WHERE id=#{1}")
	public int updateStatus(OrderStatus status, String id);

	@Select("SELECT `pay_time` , pay_type, total_fee FROM `v_order` WHERE status='PAYED' AND  `pay_time` > #{0} AND `status`='PAYED'")
	public List<Order> findLatest(Date date);

	@Select("SELECT SUM(total_fee) AS total_fee,SUM(real_fee) AS real_fee FROM `v_order` WHERE `pay_time` > #{0} AND `status`='PAYED'")
	public Order findTotalFee(Date start);
}
